package net.ecpt.dubbo.clm;

import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ClmClient {
    public static Map<String, Object> getClmAgreementClient(DispatchContext ctx, Map<String, ? extends Object> cont) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	String consumer = "ofdubbo/echo-consumer.xml";
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(consumer);
	context.start();
	// 获取远程服务代理
	ClmService clmService = (ClmService) context.getBean("clmService");
	String id = (String)cont.get("id");
	// 执行远程方法
	JSONObject params  = new JSONObject();
	params.put("login.username", "admin");
	params.put("login.password", "ecpt123456");
	params.put("id", id);
	JSONObject json = clmService.getClmAgreement(params);
	Debug.log("json===="+json);
//	result =  JSONObject.parseObject(json.toJSONString()); 
	return result;
    }
    
    
    public static Map<String, Object> listClmAgreementClient(DispatchContext ctx, Map<String, ? extends Object> cont) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	String consumer = "ofdubbo/echo-consumer.xml";
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(consumer);
	context.start();
	// 获取远程服务代理
	ClmService clmService = (ClmService) context.getBean("clmService");
	// 执行远程方法
	JSONObject params  = new JSONObject();
	params.put("login.username", "admin");
	params.put("login.password", "ecpt123456");
	//其他查询字段均可添加
	JSONObject json = clmService.listClmAgreement(params);
//	result =  JSONObject.parseObject(json.toJSONString()); 
	Debug.log("json===="+json);
	return result;
    }
    
    public static Map<String, Object> createClmCollectionOrderClient(DispatchContext ctx, Map<String, ? extends Object> cont) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	String consumer = "ofdubbo/echo-consumer.xml";
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(consumer);
	String id = (String)cont.get("id");
	context.start();
	// 获取远程服务代理
	ClmService clmService = (ClmService) context.getBean("clmService");
	// 执行远程方法
	JSONObject params  = new JSONObject();
	params.put("login.username", "admin");
	params.put("login.password", "ecpt123456");
	
	params.put("collectionCode", "2222222");	//收款编号
	params.put("typeId", "final");	//estimate 预估账单，final最终账单
	params.put("isConfirm", "N");	//确认账单
	params.put("agreementId", id);	//系统合同编号
	params.put("agreementCode", "44444444"); //外部合同编号
	params.put("customerId", ""); //相关客户
	params.put("customerCode", "");  //客户编号
	params.put("customerName", "");  //客户名称
	params.put("collectionAmount", 20000);  //收款金额
	params.put("collectionPeriod", "6");  //收款账期 
	params.put("collectionDate", getTimestamp(System.currentTimeMillis()));//收款时间
	params.put("collectionMode", "微信转账"); //收款方式
	params.put("currencyTypeId", "CNY");	//币种   CNY  USD
	params.put("rateTypeId", "");  //汇率类型
	params.put("sourceId", ""); //生成来源
	params.put("description", "本次测试收款");//备注
	params.put("corpcode", "111");	//财务组织
	params.put("billmaker", "222");	//创建人
	
	JSONArray array = new JSONArray();
	
	JSONObject ob1 = new JSONObject();
	
	ob1.put("scomment", "测试摘要01");
	ob1.put("objtype", "0");
	ob1.put("customer", "yangpeng01");
	ob1.put("dept", "");
	ob1.put("psndoc", "");
	ob1.put("recaccount", "4062540688526658");
	ob1.put("prepay", "0");	//0=应收款，1=预收款
	ob1.put("currtype", "CNY");
	ob1.put("rate", 1);
	ob1.put("money", 10000);
	ob1.put("balatype", "银行转账");
	ob1.put("inoutbusiclass", "收支项目");
	
	JSONObject ob2 = new JSONObject();
	ob2.put("scomment", "测试摘要01");
	ob2.put("objtype", "1");
	ob2.put("customer", "");
	ob2.put("dept", "1111");
	ob2.put("psndoc", "");
	ob2.put("recaccount", "4062540688526658");
	ob2.put("prepay", "0");	//0=应收款，1=预收款
	ob2.put("currtype", "CNY");
	ob2.put("rate", 1);
	ob2.put("money", 10000);
	ob2.put("balatype", "银行转账");
	ob2.put("inoutbusiclass", "收支项目");
	
	array.add(ob1);
	array.add(ob2);
	System.out.println(array.toString());
	params.put("itemList", array.toString()); 
	
	JSONObject json = clmService.createClmCollectionOrder(params);
//	result =  JSONObject.parseObject(json.toJSONString()); 
	Debug.log("json===="+json);
	return result;
    }
    
    public static String getTimestamp(long time) {
        return new java.sql.Timestamp(time).toString();
    }
}
